package monitorcon

import (
	"io/ioutil"
	"log"
	"net/http"
)

//GetresponseFromServer 发出request，从底层http server得到真正的信息
func GetresponseFromServer(url string) string {

	client := &http.Client{}
	request, err := http.NewRequest("GET", url, nil)

	if err != nil {
		log.Println("request error")
		return ""
	}

	//处理返回结果
	response, _ := client.Do(request)
	status := response.StatusCode
	if status != 200 {
		log.Println("response error")
		return ""
	}
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Println(err)
	}
	log.Println("response:", string(body))
	return string(body)

}

//GetStackinfo ...
func GetStackinfo(hostid string) string {
	var s string

	s = "{\"odata.id\":\"/redfish/v1/rich/Switches/"
	s = s + string(hostid)
	s = s + "/stack/stackInfos\","
	s = s + "\"opticalInfo\": [{"
	s = s + "\"vendorName\": \"testname\","
	s = s + "\"vendorPN\": \"pn\"}"
	s = s + ",{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\"}]"
	s = s + "}"
	
	

	//得到stack信息
	/*
		     targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostid+"/stack/stackMemberInfos"
			 s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetPortInfo ...
func GetPortInfo(hostuuid string) string {

	var s string

	s = " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/Ports\","
	s = s + "\"s@odata.count\": 2,"
	s = s + "\"Members\": ["
	s = s + "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/Ports/10GE1%2F17%2F1\","
	s = s + "\"position\": \"10GE2/17/1\""
	s = s + "},"
	s = s + " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/Ports/10GE1%2F17%2F2\","
	s = s + " \"position\": \"10GE2/17/2\","
	s = s + " \"UUID\": \"555\""
	s = s + " }"
	s = s + "]"
	s = s + "}"
	/*
			 //得到port信息
		     targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/portInfos"
			 s=GetresponseFromServer(targetUrl)
	*/
	return s

}

//GetPortByposition ...
func GetPortByposition(hostuuid string, position string) string {
	var s string

	s = "{\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/Ports/10GE1%2F17%2F1\","
	s = s + "\"position\": \"10GE2/17/1\","
	s = s + "\"portType\": \"10GF\"}"
	/*
		     //得到port信息
		     targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/portInfos/"+position
			 s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetInterfaceInfo ...
func GetInterfaceInfo(hostuuid string) string {
	var s string

	s = " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/inetrafces\","
	s = s + "\"s@odata.count\": 2,"
	s = s + "\"Members\": ["
	s = s + "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/inetrafces/10GE1%2F17%2F1\","
	s = s + "\"ifname\": \"ip\""
	s = s + "},"
	s = s + " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/inetrafces/10GE1%2F17%2F2\","
	s = s + " \"ifname\": \"ix\""
	s = s + " }"
	s = s + "]"
	s = s + "}"
	/*
			//得到interface信息
		    targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/interfaceInfos"
			s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetInterfaceByfname ...
func GetInterfaceByfname(hostuuid string, ifname string) string {

	var s string

	s = "{\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/" + hostuuid + "/Devm/Ports/10GE1%2F17%2F1\","
	s = s + "\"ifname\": \"10GE2/17/1\","
	s = s + "\"portType\": \"10GF\"}"

	//得到interface信息
	/*
		    targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/interfaceInfos/"+ifname
			s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetCPUInfo ...
func GetCPUInfo(hostuuid string) string {
	var s string

	s = " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/cpus\","
	s = s + "\"s@odata.count\": 2,"
	s = s + "\"Members\": ["
	s = s + "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/cpus/10GE1%2F17%2F1\","
	s = s + "\"position\": \"c1\""
	s = s + "},"
	s = s + " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/cpus/10GE1%2F17%2F2\","
	s = s + " \"position\": \"c2\""
	s = s + " }"
	s = s + "]"
	s = s + "}"
	/*
			//得到cpu信息
		    targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/cpuInfos"
			s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetCPUInfoByPosition ...
func GetCPUInfoByPosition(hostuuid string, position string) string {
	var s string

	s = "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/" + hostuuid + "/Devm/CpuInfos/2\","
	s = s + "\"position\": \"2\","
	s = s + "\"systemCpuUsage\": \"22\"}"
	/*
		   //得到cpu信息
		    targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/cpuInfos/"+position
			s=GetresponseFromServer(targetUrl)
	*/
	return s

}

//GetMemoryInfo ...
func GetMemoryInfo(hostuuid string) string {
	var s string

	s = " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/memorys\","
	s = s + "\"s@odata.count\": 2,"
	s = s + "\"Members\": ["
	s = s + "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/memorys/10GE1%2F17%2F1\","
	s = s + "\"position\": \"m1\""
	s = s + "},"
	s = s + " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/memorys/10GE1%2F17%2F2\","
	s = s + " \"position\": \"m2\""
	s = s + " }"
	s = s + "]"
	s = s + "}"
	/*
			//得到memory信息
		     targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/memoryInfos"
			 s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetMemoryInfoByPosition ...
func GetMemoryInfoByPosition(hostuuid string, position string) string {
	var s string

	s = "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/{HostUUID}/Devm/memory/2\","
	s = s + "\"position\":\"m2\","
	s = s + "\"osMemoryTotal\":\"22\","
	s = s + "\"osMemoryUsage\":\"12\"}"
	/*
	   //得到memory信息
	   targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/memoryInfos/"+position
	   s=GetresponseFromServer(targetUrl)
	*/
	return s

}

//GetMacInfo ...
func GetMacInfo(hostuuid string) string {
	var s string

	s = " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/macs\","
	s = s + "\"s@odata.count\": 2,"
	s = s + "\"Members\": ["
	s = s + "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/macs/10GE1%2F17%2F1\","
	s = s + "\"id\": \"mc1\""
	s = s + "},"
	s = s + " {"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/xxxx/Devm/macs/10GE1%2F17%2F2\","
	s = s + " \"id\": \"mc2\""
	s = s + " }"
	s = s + "]"
	s = s + "}"
	/*

		    //得到mac信息
		    targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/macInfos"
			s=GetresponseFromServer(targetUrl)
	*/
	return s
}

//GetMacInfoByid ...
func GetMacInfoByid(hostuuid string, id string) string {
	var s string

	s = "{"
	s = s + "\"@odata.id\": \"/redfish/v1/rich/NetworkService/Hosts/{HostUUID}/Devm/mac/1\","
	s = s + "\"id\": \"1\","
	s = s + "\"vlanId\": \"22\"}"
	/*
	   //得到mac信息
	   targetUrl := "http://localhost:8079/redfish/v1/rich/Switches/"+hostuuid+"/stack/macInfos/"+id
	   s=GetresponseFromServer(targetUrl)
	*/
	return s

}
